package ee.ttu.rakarh.model.data.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;

import ee.ttu.rakarh.model.data.entities.Item;
import ee.ttu.rakarh.model.data.entities.ItemAttribute;
import ee.ttu.rakarh.model.data.entities.ItemType;
import ee.ttu.rakarh.model.data.entities.Store;
import ee.ttu.rakarh.util.HibernateUtil;

public class StoreDAOImpl implements StoreDAO {

	private static final Logger LOG = Logger.getLogger(StoreDAOImpl.class);

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Store> getStores() {
		Session session = null;
		Collection<Store> stores = new ArrayList<Store>();
		try {
			session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			stores = session.createCriteria(Store.class).list();	
		} catch (Exception e) {
			LOG.error(e);
		} finally {
			//session.close();
		}
		return stores;
	}

	@Override
	public Collection<Store> getStoresByItemId(int itemId) {
		Session session = null;
		Collection<Store> stores = null;
		try {
			session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			
			Item item = new ItemDAOImpl().getItemById(itemId);
			Map<Store, Integer> storeHashMap = item.getItemStore();
			stores = storeHashMap.keySet();
			
			LOG.info("stores: " + stores.size());

		} catch (Exception e) {
			LOG.error(e);
		} finally {
			//session.close();
		}
		return stores;
	}

	@Override
	public Store getStore(int storeId) {
		Session session = null;
		Store store = null;
		try {
			session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			store = (Store) session.load(Store.class, storeId);	
		} catch (Exception e) {
			LOG.error(e);
		} finally {
			//session.close();
		}
		return store;
	}


}
